class AmapWX {
    constructor(options) {
        this.key = options.key;
        this.requestConfig = {
            key: options.key,
            s: 'rsx',
            platform: 'WXJS',
            appname: options.key,
            sdkversion: '1.2.0',
            logversion: '2.0'
        };
    }

    getWxLocation(type, success, fail) {
        type = type || 'gcj02';
        success = success || function () { };
        fail = fail || function () { };
        uni.getLocation({
            type: type,
            success: success,
            fail: fail
        });
    }

    getRegeo(options) {
        options = options || {};
        let that = this;
        let requestParam = {
            ...this.requestConfig,
            location: options.location,
            extensions: 'all',
            output: 'json',
        };

        uni.request({
            url: 'https://restapi.amap.com/v3/geocode/regeo',
            data: requestParam,
            method: 'GET',
            header: { 'content-type': 'application/json' },
            success: function (res) {
                if (res.data.status === '1' && res.data.info === 'OK') {
                    let addressComponent = res.data.regeocode.addressComponent;
                    options.success && options.success({
                        name: res.data.regeocode.formatted_address,
                        desc: res.data.regeocode.formatted_address,
                        address: addressComponent.province + addressComponent.city + addressComponent.district,
                        latitude: options.location.split(',')[1],
                        longitude: options.location.split(',')[0]
                    });
                } else {
                    options.fail && options.fail({
                        errCode: res.data.infocode,
                        errMsg: res.data.info
                    });
                }
            },
            fail: function (res) {
                options.fail && options.fail({
                    errCode: 0,
                    errMsg: res.errMsg || '请求失败'
                });
            }
        });
    }

    getDrivingRoute(options) {
        options = options || {};
        let requestParam = {
            ...this.requestConfig,
            origin: options.origin,
            destination: options.destination,
            waypoints: options.waypoints || '',
            extensions: 'all',
            strategy: options.strategy || 0,
            output: 'json',
        };

        uni.request({
            url: 'https://restapi.amap.com/v3/direction/driving',
            data: requestParam,
            method: 'GET',
            header: { 'content-type': 'application/json' },
            success: function (res) {
                if (res.data.status === '1' && res.data.info === 'OK') {
                    let points = [];
                    if (res.data.route && res.data.route.paths) {
                        let path = res.data.route.paths[0];
                        let steps = path.steps;
                        steps.forEach(step => {
                            let polyline = step.polyline.split(';');
                            polyline.forEach(point => {
                                let lnglat = point.split(',');
                                points.push({
                                    longitude: parseFloat(lnglat[0]),
                                    latitude: parseFloat(lnglat[1])
                                });
                            });
                        });
                    }
                    options.success && options.success({
                        points: points,
                        distance: res.data.route.paths[0].distance,
                        duration: res.data.route.paths[0].duration
                    });
                } else {
                    options.fail && options.fail({
                        errCode: res.data.infocode,
                        errMsg: res.data.info
                    });
                }
            },
            fail: function (res) {
                options.fail && options.fail({
                    errCode: 0,
                    errMsg: res.errMsg || '请求失败'
                });
            }
        });
    }
}

export default AmapWX;